//
//  DeviceInfo.m
//  etionMisc
//
//  Created by wu jingxing on 12-12-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DeviceInfo.h"

#import <sys/sysctl.h>

#import <mach/mach.h>

@implementation UIDevice (DeviceInfo)

+(CGSize)screenSize
{
    return [UIScreen mainScreen].bounds.size;
}

+(CGFloat)screenWidth
{
    return [UIScreen mainScreen].bounds.size.width;
}

+(CGFloat)screenHeight
{
    return [UIScreen mainScreen].bounds.size.height;
}

+(BOOL)is35InchScreen
{
    return [UIDevice screenSize].height<=480?YES:NO;
}

+(BOOL)is40InchScreen
{
    return [UIDevice screenSize].height==568?YES:NO;
}

+(BOOL)is47InchScreen
{
    return [UIDevice screenSize].height==667?YES:NO;
}

+(BOOL)is55InchScreen
{
    return [UIDevice screenSize].height==736?YES:NO;
}

+(BOOL)is58InchScreen
{
    return [UIDevice screenSize].height==812?YES:NO;
}

+(BOOL)isIOS11OrNewer
{
    return [[UIDevice currentDevice].systemVersion floatValue]>=11.0?YES:NO;
}

+(BOOL)isIOS9
{
    NSRange range = [[UIDevice currentDevice].systemVersion rangeOfString:@"9.0"];
    return range.location != NSNotFound ? YES : NO;
}

+(BOOL)isIOS9Serial
{
    return [[UIDevice currentDevice].systemVersion integerValue]==9?YES:NO;
}

+(BOOL)isIOS9OrNewer
{
    return [[UIDevice currentDevice].systemVersion floatValue]>=9.0?YES:NO;
}

+(BOOL)isIOS8Serial
{
    return [[UIDevice currentDevice].systemVersion integerValue]==8?YES:NO;
}

+(BOOL)isIOS8OrNewer
{
    return [[UIDevice currentDevice].systemVersion floatValue]>=8.0?YES:NO;
}

+(BOOL)isIOS7Serial
{
    return [[UIDevice currentDevice].systemVersion integerValue]==7?YES:NO;
}

+(BOOL)isIOS7OrNewer
{
    return [[UIDevice currentDevice].systemVersion floatValue]>=7.0?YES:NO;
}

+(BOOL)isIOS6Serial
{
    return [[UIDevice currentDevice].systemVersion integerValue]==6?YES:NO;
}

+(BOOL)isIOS6OrNewer
{
    return [[UIDevice currentDevice].systemVersion floatValue]>=6.0?YES:NO;
}

+(NSUInteger)currentAvailableMemory
{
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO64_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    
    if(kernReturn != KERN_SUCCESS)
        return NSNotFound;
    
    return vm_page_size*vmStats.free_count / 1024 / 1024;
}

+(NSUInteger)currentUsedMemory
{
    task_basic_info_data_t taskInfo;
    mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
    kern_return_t kernReturn = task_info(mach_task_self(),TASK_BASIC_INFO,(task_info_t)&taskInfo,&infoCount);
    
    if (kernReturn != KERN_SUCCESS)
        return NSNotFound;
    
    return taskInfo.resident_size / 1024.0 / 1024.0;
}

@end
